package com.zjitc.redis;

import com.google.gson.Gson;
import com.zjitc.dao.test.TESTDao;
import com.zjitc.model.test.TESTUser;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Random;
import java.util.Set;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import redis.clients.jedis.Jedis;

/**
 * Create by IntelliJ IDEA
 *
 * @author: jsonor
 * @date-Time: 2018/4/3 15:06
 * @description:
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"classpath:application-config.xml"})
public class Redistest {

  private static final Integer MAX = 2000;

  private static final Integer bound = 500;

  @Autowired
  private Jedis jedis;

  @Autowired
  private TESTDao dao;

  private Gson gson = new Gson();


  //@BeforeClass
  //public static void t1() {
  //  //连接本地的 redis 服务
  //  jedis = new Jedis();
  //  System.out.println("连接成功");
  //  //查看服务是否运行
  //  System.out.println("服务正在运行: " + jedis.ping());
  //}

  //@Test
  //public void t3(){
  //  int i1 = new Random().nextInt(100);
  //  System.out.println(i1);
  //}

  @Test
  public void t2() {
    int i = 0;
    System.out.println(System.currentTimeMillis());
    List<TESTUser> userList = new ArrayList<>();
    while (i < MAX) {
      if (i % 2 == 0) {
        TESTUser u1 = new TESTUser(i, new Random().nextInt(100), "m");
        userList.add(u1);
        t4(u1);
        t6(u1);
      }else {
        TESTUser u2 = new TESTUser(i, new Random().nextInt(100), "w");
        userList.add(u2);
        t4(u2);
        t6(u2);
      }
      i++;
      if(i % bound == 0){
        dao.adduser(userList);
        userList.removeAll(userList);
      }
      if(i == MAX){
        if(!userList.isEmpty()){
          dao.adduser(userList);
        }
      }
    }
    System.out.println(System.currentTimeMillis());
  }

  final String AGE_SMALL_24 = "AGE_SMALL_24";
  final String SEX_IS_M = "SEX_IS_M";

  private void t4(TESTUser user) {
    if(user.getAge() < 24){
      jedis.sadd(AGE_SMALL_24,user.getId().toString());
    }

    if(user.getSex().equals("m")){
      jedis.sadd(SEX_IS_M,user.getId().toString());
    }
  }

  public void t6(TESTUser user){
    HashMap<String, String> map = new HashMap<>();
    map.put("age",user.getAge().toString());
    map.put("sex",user.getSex());
    jedis.hmset(user.getId().toString(),map);
  }


  /**
   * 测试存入redis的时间
   */
  @Test
  public void t3() {
    int i = 0;
    List<TESTUser> users = dao.findAll();
    Gson gson = new Gson();
    System.out.println(System.currentTimeMillis());
    for (TESTUser u : users) {
      String s = gson.toJson(u);
      jedis.sadd(String.valueOf(u.getId()),s);
      jedis.expire(String.valueOf(u.getId()),300);
    }
    System.out.println(System.currentTimeMillis());
  }



  @Test
  public void t5(){
    dao.del();
  }


  @Test
  public void testredis(){
    Long scard = jedis.scard(AGE_SMALL_24);
    System.out.println(scard);
    scard = jedis.scard(SEX_IS_M);
    System.out.println(scard);

    Set<String> sinter = jedis.sinter(AGE_SMALL_24, SEX_IS_M);
    System.out.println(sinter);
  }


}
